<?php

/**
 * @package Framework03
 * @author Walter Tamboer
 */

/**
 * The session jar manages sessions.
 */
final class SessionJar
{
	/**
	 * The current session ID.
	 */
	public $ID;
	/**
	 * Initaliizes a new instance of this class.
	 */
	public function __construct()
	{
		// Start sessions
		session_start();
		
		$this->ID = session_id();
	}
	/**
	 * Cleans up all resources used by this class.
	 */
	public function __destruct()
	{
		session_write_close();
	}
	/**
	 * Regenerates the current session ID.
	 * @param bool $DeleteOld True if the old ID should be deleted; false otherwise.
	 * @return string Returns the new session ID.
	 */
	public function RegenerateId($DeleteOld = false)
	{
		session_regenerate_id($DeleteOld);

		$this->ID = session_id();

		return $this->ID;
	}
	/**
	 * Deletes a session.
	 * @param string $Name The name of the session to delete.
	 */
	public function Delete($Name)
	{
		if (!isset($_SESSION))
			return false;

		if (!isset($_SESSION[$Name]))
			return false;

		unset($_SESSION[$Name]);
	}
	/**
	 * Gets a session.
	 * @param string $Name The name of the session to get.
	 * @return Session Returns the session object.
	 */
	public function Get($Name)
	{
		if (!isset($_SESSION))
			return null;

		if (!isset($_SESSION[$Name]))
			return null;

		$Session = new Session;
		$Session->SetID($this->ID);
		$Session->SetName($Name);
		$Session->SetData(unserialize($_SESSION[$Name]));

		return $Session;
	}
	/**
	 * Sets the session.
	 * @param string $Name The name of the session to set.
	 * @param mixed $Value The value of the session.
	 */
	public function Set($Name, $Value)
	{
		$_SESSION[$Name] = serialize($Value);
	}
}

?>